// controller/tags.js
"use strict";
const Controller = require("egg").Controller;
const moment = require("moment");
class TagsController extends Controller {
  // 创建标签
  async newTags() {
    const { ctx } = this;
    ctx.logger.info("创建标签参数", ctx.request.body);
    const data = {
      ...ctx.request.body,
      create_time: moment().format("YYYY-MM-DD HH:mm:ss"),
      status: ctx.request.body.status == undefined ? 0 : ctx.request.body.status,
    };
    const result = await ctx.service.tags.newTags(data);
    console.log("result", result);
    if (!result) {
      ctx.body = {
        code: 500,
        message: "创建失败",
        success: false,
      };
      return;
    }
    ctx.body = {
      code: 200,
      message: "创建成功",
      success: true,
    };
  }
  // 删除标签
  async deleteTags() {
    const { ctx } = this;
    ctx.logger.info("删除标签参数", ctx.request.body);
    const { id } = ctx.request.body;
    const result = await ctx.service.tags.deleteTags(id);
    console.log("result", result);
    if (!result) {
      ctx.body = {
        code: 500,
        message: "删除失败",
        success: false,
      };
      return;
    }
    ctx.body = {
      code: 200,
      message: "删除成功",
      success: true,
    };
  }
  // 更新标签
  async updateTags() {
    const { ctx } = this;
    ctx.logger.info("更新标签参数", ctx.request.body);
    const data = ctx.request.body;
    const result = await ctx.service.tags.updateTags(data);
    console.log("result", result);
    if (!result || result.changedRows == 0) {
      ctx.body = {
        code: 500,
        message: "更新失败",
        success: false,
      };
      return;
    }
    ctx.body = {
      code: 200,
      message: "更新成功",
      success: true,
    };
  }
  // 标签列表
  async getTagsList() {
    const { ctx } = this;
    ctx.logger.info("标签列表参数", ctx.request.query);
    const data = ctx.request.query;
    const result = await ctx.service.tags.getTagsList(data);
    console.log("result", result);
    if (!result) {
      ctx.body = {
        code: 500,
        message: "查询失败",
        success: false,
      };
      return;
    }
    ctx.body = result;
  }
  // 标签详情
  async getTagsDetail() {
    const { ctx } = this;
    ctx.logger.info("标签详情参数", ctx.request.query);
    const { id } = ctx.request.query;
    const result = await ctx.service.tags.getTagsDetail(id);
    console.log("result", result);
    if (!result) {
      ctx.body = {
        code: 500,
        message: "查询失败",
        success: false,
      };
      return;
    }
    ctx.body = {
      code: 200,
      success: true,
      data: result,
    };
  }
}

module.exports = TagsController;
